package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.R;
import com.reggie.common.ReggieConstants;
import com.reggie.dto.DishDto;
import com.reggie.entity.Dish;
import com.reggie.entity.ShoppingCart;
import com.reggie.service.DishService;
import com.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("/page")
    public R<Page<DishDto>> page(int page, int pageSize, String name) {
        Page<DishDto> dishDtoPage = dishService.pageDish(page, pageSize, name);
        return R.success(dishDtoPage);
    }

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        dishService.saveDish(dishDto);
        Set keys = redisTemplate.keys(ReggieConstants.DISH_LIST_REDIS_PREFIX + "*");
        redisTemplate.delete(keys);
        return R.success("success");
    }

    @GetMapping("/{id}")
    public R<DishDto> findById(@PathVariable Long id) {
        DishDto byId = dishService.findById(id);
        return R.success(byId);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        dishService.updateDish(dishDto);
        Set keys = redisTemplate.keys(ReggieConstants.DISH_LIST_REDIS_PREFIX + "*");
        redisTemplate.delete(keys);
        return R.success("修改成功");
    }

    @DeleteMapping
    public R<String> delete(@RequestParam("ids") List<Long> ids) {
        dishService.deleteDish(ids);
        Set keys = redisTemplate.keys(ReggieConstants.DISH_LIST_REDIS_PREFIX + "*");
        redisTemplate.delete(keys);
        return R.success("删除成功");

    }

    @PostMapping("/status/{statu}")
    @Transactional(rollbackFor = Exception.class)
    public R<String> updateStatus(@PathVariable int statu, @RequestParam("ids") List<Long> ids) {
        if (ids != null) {
            LambdaUpdateWrapper<Dish> dishLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            dishLambdaUpdateWrapper.in(Dish::getId, ids);
            dishLambdaUpdateWrapper.set(Dish::getStatus, statu);
            dishLambdaUpdateWrapper.set(Dish::getUpdateTime, LocalDateTime.now());
            //如果要设置的状态为0，说明是下架商品，下架商品则将购物车里对应的数据也给删掉
            if (statu == 0) {
                LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
                shoppingCartLambdaQueryWrapper.in(ShoppingCart::getDishId, ids);
                shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
            }
            Set keys = redisTemplate.keys(ReggieConstants.DISH_LIST_REDIS_PREFIX + "*");
            redisTemplate.delete(keys);
            dishService.update(dishLambdaUpdateWrapper);
        }
        return R.success("操作成功");
    }

    @GetMapping("/list")
    public R<List<DishDto>> list(Long categoryId, String name, Integer status) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        List<DishDto> dishDtos = (List<DishDto>) valueOperations.get(ReggieConstants.DISH_LIST_REDIS_PREFIX+categoryId + ":" + name + ":" + status);
        if (dishDtos!=null){
            return R.success(dishDtos);
        }
        else {
            List<DishDto> dishDtoList = dishService.listDish(categoryId, name, status);
            valueOperations.set(ReggieConstants.DISH_LIST_REDIS_PREFIX+categoryId + ":" + name + ":" + status,dishDtoList);
            return R.success(dishDtoList);
        }


    }
}
